<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/11/26
 * Time: 13:48
 */
namespace app\admin\controller;

use think\Controller;
use think\Request;
use think\Db;
use think\Session;
use app\admin\model\Banner as BannerModel;
class Circle extends Common
{
    /*
    * 活动栏目列表
    */
    public function label()
    {

        //获取数据库的信息用于展示
        if (Request::instance()->isAjax()) {
            $pagedata = Request::instance()->get();
            $pagesize = $pagedata['limit'];
            $page = (int)$pagedata['page'];
            if ($page == 1) {
                $startrow = 0;
            } else {
                $startrow = ($page - 1) * $pagesize;
            }
            $where['is_display'] = array('eq', 0);
            if (isset($pagedata['user_tel']) && $pagedata['user_tel'] != '') {
                $where['data_name'] = array('like', '%' . $pagedata['user_tel'] . '%');
            }
            $count = Db::name('fc_tags')->where($where)->count();
            $data = Db::name('fc_tags')->field("id,data_name,data_sort")
                ->limit($startrow, $pagesize)
                ->where($where)
                ->order('id', 'desc')
                ->select();
            return json(array('code' => 0, 'msg' => '请求成功', 'count' => $count, 'data' => $data));

        } else {
            return $this->fetch();
        }

    }

    /*
     * 添加活动标签
     */
    public function addColumn()
    {
        if (Request::instance()->isAjax()) {
            $post = Request::instance()->post();
            $post = $post['data'];
            //检查用户名是否存在
            $check = Db::name('fc_tags')->where("data_name", $post['data_name'])->find();
            if ($check) {
                return ['code' => '圈友栏目已经存在'];
                exit;
            }
            $data = [
                'data_name' => $post['data_name'],
                'data_sort' => $post['data_sort'],

            ];

            $res = Db::name('fc_tags')->insert($data);

            if ($res) {
                return ['code' => 0];
            } else {
                return ['code' => '添加失败'];
            }
        }

    }
    /**
     * [addQuickComment 添加快捷评论]
     * @Author   kendy
     * @DateTime 2020-04-23T08:47:52+0800
     */
    public function addQuickComment(){
       if (Request::instance()->isAjax()) {
            $post = Request::instance()->post();
            $post = $post['data']; 
            $data = [
                'content' => $post['content'],  
                'create_time' => date('Y-m-d H:i:s',time()), 
            ];

            $res = Db::name('quick_comments')->insert($data);

            if ($res) {
                return ['code' => 0];
            } else {
                return ['code' => '添加失败'];
            }
        }
    }

    /*
     * 删除活动标签
     */
    public function delColumn()
    {
        if (Request::instance()->isAjax()) {
            $post = Request::instance()->post();
            $res = Db::name('fc_tags')->where('id', $post['id'])->delete();
            if ($res !== false) {
                return "1";
            } else {
                return "2";
            }
        }
    }

    /*
     * 双击修改权重
     */
    public function editSort()
    {
        if (Request::instance()->isAjax()) {
            $post = Request::instance()->post();
            $res = Db::name('fc_tags')->where('id', $post['id'])->setField('data_sort', $post['sort']);
            if ($res !== false) {
                return ['code' => 0];
            } else {
                return ['code' => '修改失败'];
            }
        }
    }

    /*
     * 修改活动标签
     */
    public function editColumn()
    {
        if (Request::instance()->isAjax()) {
            $post = Request::instance()->post();
            $saveData = array(
                'data_name' => $post['data']['data_name'],
            );

            $where['data_name'] = array('eq', $post['data']['data_name']);
            //检查用户名是否存在
            $check = Db::name('fc_tags')->where($where)->find();
            if ($check) {
                return ['code' => '圈友标签已经存在'];
                exit;
            }

            $res = Db::name('fc_tags')->where('id', $post['data']['id'])->update($saveData);

            if ($res !== false) {
                return ['code' => 0];
            } else {
                return ['code' => 1];
            }
        }
    }

    /*
     * 图文列表展示
     */
    public function image()
    {
        //获取数据库的信息用于展示
        if (Request::instance()->isAjax()) {
            $pagedata = Request::instance()->get();
            $pagesize = $pagedata['limit'];
            $page = (int)$pagedata['page'];
            if ($page == 1) {
                $startrow = 0;
            } else {
                $startrow = ($page - 1) * $pagesize;
            }
            $where['a.is_del'] = array('eq', 0);
            $where['a.status'] = array('eq', 1);
            if (isset($pagedata['user_tel']) && $pagedata['user_tel'] != '') {
                $where['fc_title'] = array('like', '%' . $pagedata['user_tel'] . '%');
            }
            if (isset($pagedata['data_name']) && $pagedata['data_name'] != '') {
                $where['data_name'] = array('like', '%' . $pagedata['data_name'] . '%');
            }
            $count = Db::name('friends_circle')->alias('a')->field('a.*,f.data_name,u.nickname')->join('fc_tags f', 'a.fc_id=f.id', 'left')
                ->join('user u', 'a.uid=u.uid', 'left')->where($where)->count();
            $data = Db::name('friends_circle')->alias('a')->field('a.*,f.data_name,u.nickname')->join('fc_tags f', 'a.fc_id=f.id', 'left')
                ->join('user u', 'a.uid=u.uid', 'left')
                ->limit($startrow, $pagesize)
                ->where($where)
                ->order('f_id', 'desc')
                ->select();
                return json(array('code' => 0, 'msg' => '请求成功', 'count' => $count, 'data' => $data));

        } else {
//            $res = Db::name('friends_circle')->alias('a')->field('a.*,f.data_name,u.nickname')->join('fc_tags f', 'a.fc_id=f.id', 'left')
//                ->join('user u', 'a.uid=u.uid', 'left')
//                ->where('f.is_display',0)
//                ->select();

            $res = Db::name('fc_tags')->field('data_name')->where('is_display',0)->select();
            $this->assign('res',$res);
            return $this->fetch();
        }
    }

    /*
     * 审核
     */
    public function circleIsPass()
    {
        if (request()->isAjax()){
            $postData = input('param.');
            $where = "f_id = {$postData['id']}";
            $data['is_pass'] = $postData['is_pass'];
            $data['update_time'] = date('Y-m-d H:i:s');
            $res = Db::name('friends_circle')->where($where)->update($data);
            if($res){
                if($postData['is_pass']==1){
                    //发送系统消息
                    $data = [
                        'msg_type'=>1,
                        'receive_user_id'=>$postData['uid'],
                        'msg_title' =>'发表成功!',
                        'msg_content'=>'恭喜你发表的'.$postData['title'].'已通过审核，感谢你的记录分享',
                        'create_time'=>date('Y-m-d H:i:s'),
                    ];
                }else{
                    $data = [
                        'msg_type'=>1,
                        'receive_user_id'=>$postData['uid'],
                        'msg_title' =>'发表失败!',
                        'msg_content'=>'很抱歉你发表的'.$postData['title'].'未通过审核，感谢你的记录分享',
                        'create_time'=>date('Y-m-d H:i:s'),
                    ];
                }

                Db::name('message')->insert($data);

                return 0;
            }else{
                return 1;
            }
        }
    }

    /*
     * 圈友文章删除
     */
    public function delCircle()
    {
        if (Request::instance()->isAjax()) {
            $post = Request::instance()->post();
            $res = Db::name('friends_circle')->where('f_id', $post['id'])->setField('is_del',1);
            if ($res !== false) {
                return "1";
            } else {
                return "2";
            }
        }
    }

    /*
     * 修改基础信息
     */
    public function editCircle()
    {
        if (Request::instance()->isAjax()) {
            $post = Request::instance()->post();
            $saveData = array(
                'set_like_num' => $post['data']['set_like_num'],
                'set_collect_num' => $post['data']['set_collect_num'],
                'set_share_num' => $post['data']['set_share_num'],
            );

            $res = Db::name('friends_circle')->where('f_id', $post['data']['id'])->update($saveData);

            if ($res !== false) {
                return ['code' => 0];
            } else {
                return ['code' => 1];
            }
        }
    }

    /*
     * 修改评论信息
     */
    public function editquickcomments()
    {
        if (Request::instance()->isAjax()) {
            $post = Request::instance()->post();
            $saveData = array(
                'content' => $post['data']['content'], 
            );

            $res = Db::name('quick_comments')->where('id', $post['data']['id'])->update($saveData);

            if ($res !== false) {
                return ['code' => 0];
            } else {
                return ['code' => 1];
            }
        }
    } 
    public function delQuickComment(){
      if (Request::instance()->isAjax()) {
            $post = Request::instance()->post();
            $res = Db::name('quick_comments')->where('id', $post['id'])->setField('is_del',1);
            if ($res !== false) {
                return "1";
            } else {
                return "2";
            }
        }
    }

    /**
     * 视频列表
     */
    public function video()
    {
        //获取数据库的信息用于展示
        if (Request::instance()->isAjax()) {
            $pagedata = Request::instance()->get();
            $pagesize = $pagedata['limit'];
            $page = (int)$pagedata['page'];
            if ($page == 1) {
                $startrow = 0;
            } else {
                $startrow = ($page - 1) * $pagesize;
            }
            $where['a.is_del'] = array('eq', 0);
            $where['a.status'] = array('eq', 2);
            if (isset($pagedata['user_tel']) && $pagedata['user_tel'] != '') {
                $where['fc_title'] = array('like', '%' . $pagedata['user_tel'] . '%');
            }
            if (isset($pagedata['data_name']) && $pagedata['data_name'] != '') {
                $where['data_name'] = array('like', '%' . $pagedata['data_name'] . '%');
            }
            $count = Db::name('friends_circle')->alias('a')->field('a.*,f.data_name,u.nickname')->join('fc_tags f', 'a.fc_id=f.id', 'left')
                ->join('user u', 'a.uid=u.uid', 'left')->where($where)->count();
            $data = Db::name('friends_circle')->alias('a')->field('a.*,f.data_name,u.nickname')->join('fc_tags f', 'a.fc_id=f.id', 'left')
                ->join('user u', 'a.uid=u.uid', 'left')
                ->limit($startrow, $pagesize)
                ->where($where)
                ->order('f_id', 'desc')
                ->select();
            return json(array('code' => 0, 'msg' => '请求成功', 'count' => $count, 'data' => $data));

        } else {
            $res = Db::name('fc_tags')->field('data_name')->where('is_display',0)->select();
            $this->assign('res',$res);
            return $this->fetch();
        }
    }



  /**
     * 快捷评论列表
     */
    public function quickcomment()
    {
        //获取数据库的信息用于展示
        if (Request::instance()->isAjax()) {
            $pagedata = Request::instance()->get();
            $pagesize = $pagedata['limit'];
            $page = (int)$pagedata['page'];
            if ($page == 1) {
                $startrow = 0;
            } else {
                $startrow = ($page - 1) * $pagesize;
            }
            $where['is_del'] = array('eq', 0);  
            if (isset($pagedata['content']) && $pagedata['content'] != '') {
                $where['content'] = array('like', '%' . $pagedata['content'] . '%');
            } 

            $count =  Db::name('quick_comments')
            ->where($where)
            ->count();
            $data = Db::name('quick_comments')
            ->where($where)
            ->order('id desc')
            ->select(); 
            return json(array('code' => 0, 'msg' => '请求成功', 'count' => $count, 'data' => $data));

        } else { 
            return $this->fetch();
        }
    }

      /**
     * 显示不显示
     */
    public function isCommentShow()
    {
        if(Request::instance()->isAjax()){
            $post = Request::instance()->post();
            $res = Db::name('quick_comments')->where('id',$post['id'])->setField('is_display',$post['is_display']);

            if ($res!==false){
                return ['code'=>1];
            }else{
                return ['code'=>2];
            }
        }
    }

    /**
     * 显示不显示
     */
    public function isShow()
    {
        if(Request::instance()->isAjax()){
            $post = Request::instance()->post();
            $res = Db::name('friends_circle')->where('f_id',$post['id'])->setField('is_display',$post['is_display']);

            if ($res!==false){
                return ['code'=>1];
            }else{
                return ['code'=>2];
            }
        }
    }



    
}